import { Body, Controller, Get, Post, Query } from '@nestjs/common';
import { StoryService } from './story.service';
import { Public } from '../auth/auth.guard';
import { ApiOperation, ApiTags } from '@nestjs/swagger';

@ApiTags('story 模块')
@Controller('story')
export class StoryController {
  constructor(private readonly storyService: StoryService) {}
  @ApiOperation({ summary: '获取最新新闻' })
  @Public()
  @Get('news_latest')
  async getNewsLatest() {
    const stories = await this.storyService.queryNewsLatest();
    return {
      top_stories: stories[0].stories,
      date: stories[0].date,
      stories,
    };
  }

  @ApiOperation({ summary: '获取往期新闻' })
  @Public()
  @Get('news_before')
  async getNewsBefore(@Query('time') time: string) {
    console.log(time);
    const stories = await this.storyService.queryNewsBefore(time);
    if (stories.length) {
      return {
        date: stories[0].date,
        stories,
      };
    }
    return {};
  }

  @ApiOperation({ summary: '获取新闻详情' })
  @Public()
  @Get('news_info')
  async getNewsDetail(@Query('id') id: string) {
    return await this.storyService.queryNewsDetail(id);
  }
  @Public()
  @Get('story_extra')
  async getStoryExtra(@Query('id') id: string) {
    return await this.storyService.queryStoryExtra(id);
  }
  @Public()
  @ApiOperation({ summary: '保存新闻' })
  @Post('save')
  async saveNews(@Body() data: any) {
    return await this.storyService.insertNews(data.url);
  }
}
